<script type="text/javascript">
	function doSearchOrder(value) {
		
	}
	
	//表格查询  
	function reloadOrder() {
		var params = order_list.datagrid('options').queryParams; //先取得 datagrid 的查询参数  
		printLog(params);
		order_list.datagrid('reload'); //设置好查询参数 reload 一下就可以了  
		order_list.datagrid('unselectAll');
		order_list.datagrid('uncheckAll');
	}


	function addOrder() {
// 		createFromWindow("新增信息", "order.do?editOrder", "order.do?saveOrder",
// 				function(data) {
// 					reloadOrder();
// 				},true,1050,600);
		createFromWindowOptions({
			title:"新增信息",
			maximizable:true,
			width:1300,
			height:600,
			href:"order.do?editOrder",
			callback:function(d) {
				var data = d.form("getData");
				var itemDatas = $('#orderItem_list').datagrid('getRows');
				if(itemDatas.length==0) {
					showMsg('请添加单据内容');
				}else {
					data.items = itemDatas;
					 $.post('order.do?createOrder',{data:JSON.stringify(data)},function(result){
		                    if (result.success){
		                    	reloadOrder();    
		                    }
		                    showMsg(result.msg);
		                },'json');
				}
				
// 				printLog(data);
				
			},
			enableSaveButton:true
		});
	}
	
	function deleteOrder() {
		var rows = order_list.datagrid('getChecked');
	    if (rows&&rows.length>0){
	    	for(var i=0; i<rows.length; i++){
        		if(rows[0].status!='1') {
    				showMsg(rows[0].no+'为非创建状态订单，无法删除，请确认订单状态!',2500);
    				return;
    			}
        	}
	    	
	        $.messager.confirm('询问','你确认要删除吗?',function(r){
	            if (r){
	            	var ids = "";
	            	for(var i=0; i<rows.length; i++){
	            		ids += rows[i].id+",";
	            	}
	                $.post('order.do?deleteOrderItems',{ids:ids},function(result){
	                    if (result.success){
	                    	reloadOrder();    
	                    }
	                    showMsg(result.msg);
	                },'json');
	            }
	        });
	    }else {
	    	showMsg("请选择操作记录.");
	    }
	}

	function editOrder() {
		var rows = order_list.datagrid('getChecked');
		printLog(rows);
		if (rows&&rows.length>0) {
			if(rows.length>1) {
				showMsg('请选择一条操作记录.');
				return;
			}
// 			createFromWindow("编辑信息", "order.do?editOrder&orderId=" + rows[0].id,
// 					"order.do?saveOrder", function(data) {
// 						reloadOrder();
// 					},true,1050,600);
// 			if(rows[0].status!='1') {
// 				showMsg('非创建状态订单无法编辑，请确认订单状态!',2500);
// 				return;
// 			}
			createFromWindowOptions({
				title:"编辑信息",
				maximizable:true,
				width:1050,
				height:600,
				href:"order.do?editOrder&orderId=" + rows[0].id,
				callback:function(d) {
					var data = d.form("getData");
					var itemDatas = $('#orderItem_list').datagrid('getRows');
					if(itemDatas.length==0) {
						showMsg('请添加单据内容');
					}else {
						data.items = itemDatas;
						 $.post('order.do?createOrder',{data:JSON.stringify(data)},function(result){
			                    if (result.success){
			                    	reloadOrder();    
			                    }
			                    showMsg(result.msg);
			                },'json');
					}
				},
				enableSaveButton:rows[0].status=='1'
			});
		} else {
			showMsg('请选择操作记录.');
		}

	}
	
	
	//操作按钮触发
	function operationsPrintOrder(id) {
		if (id == undefined) {//点击右键菜单才会触发这个
			var rows = order_list.datagrid('getSelections');
			id = rows[0].id;
		}
		if (id) {
			createFromWindow("查看信息", "order.do?editOrder&orderId=" + id,
					null, null,false);
		} else {
			showMsg('请选择查看记录.');
		}
	}
	
	function operationsEditOrder(id) {
		if (id == undefined) {//点击右键菜单才会触发这个
			var rows = order_list.datagrid('getSelections');
			id = rows[0].id;
		}
		if (id) {
			createFromWindow("编辑信息", "order.do?editOrder&orderId=" + id,
					"order.do?saveOrder", function(data) {
						reloadOrder();
					},true);
		} else {
			showMsg('请选择编辑记录.');
		}
	}
	
	function operationsSendOrder() {
		printLog('发送');
	}
	
	function operationsDeleteOrder(id) {
		if (id == undefined) {//点击右键菜单才会触发这个
			var rows = new_list.datagrid('getSelections');
			id = rows[0].id;
		}
		if (id) {
			$.messager.confirm('询问', '你确认要删除吗?', function(r) {
				if (r) {
					$.post('order.do?deleteOrderItems', {
						ids : id
					}, function(result) {
						if (result.success) {
							reloadOrder(); 
						}
						showMsg(result.msg);
					}, 'json');
				}
			});
		} else {
			showMsg("请选择操作记录.");
		}
	}
	
	function clearOrderSearch() {
		setGridSearchParams('order_list','orderSearchForm',true,reloadOrder);
	}
	
	//根据条件查询
	function orderSearch() {
		setGridSearchParams('order_list','orderSearchForm',false,reloadOrder);
	}
	
	function printOrder() {
		var rows = order_list.datagrid('getChecked');
	    if (rows&&rows.length>0){
	    	if(rows.length>1) {
				showMsg('请选择一条订单进行打印.');
				return;
			}
           	window.open('order.do?print&orderId='+rows[0].id, '_blank');
	    }else {
	    	showMsg("请选择操作记录.");
	    }
	}
	
	function tracingOrder() {
		var rows = order_list.datagrid('getChecked');
	    if (rows&&rows.length>0){
           	var ons = "";
           	for(var i=0; i<rows.length; i++){
           		ons += rows[i].no+"\r\n";
      		}
           	window.tabsParams=ons;
    		printLog(window.tabsParams);
    		
    		var isExists = window.mainpage.mainTabs.isExists(
    				'查看跟踪', 'orderTracing.do?orderTracing');
    		printLog(isExists);
    		window.mainpage.clickMenuAddModelTab('',
    				'查看跟踪', '',
    				'orderTracing.do?orderTracing', 'icon-save', false);
    		
    		
    		if (isExists == 2) {
    			var currentTab = $('#mainTabs').tabs('getSelected');
    			currentTab.panel('refresh', 'orderTracing.do?orderTracing');
    			printLog(currentTab);
    		}
	    }else {
	    	showMsg("请选择操作记录.");
	    }
	    
		
	}
	function checkOrder(actionStatus) {
		var rows = order_list.datagrid('getChecked');
		if (rows && rows.length > 0) {
			var ids = "";
			for ( var i = 0; i < rows.length; i++) {
				if (rows[i].status != '1'&&actionStatus==2) {
					//非新增无法审核
					showMsg(rows[i].no + '为非创建/待订单审核状态的订单，无法进行订单审核，请确认订单状态！', 2500);
					return;
				}
				if (rows[i].status != '2'&&actionStatus==3) {
					showMsg(rows[i].no + '为非待财务审核状态的订单，无法进行财务审核，请确认订单状态！', 2500);
					return;
				}
				if (rows[i].status != '3'&&actionStatus==4) {
					showMsg(rows[i].no + '为非待出库审核状态的订单，无法进行出库审核，请确认订单状态！', 2500);
					return;
				}
				if (rows[i].status != '4'&&actionStatus==5) {
					showMsg(rows[i].no + '为非待发货确认状态的订单，无法进行发货确认，请确认订单状态！', 2500);
					return;
				}
				if (rows[i].status != '5'&&actionStatus==6) {
					showMsg(rows[i].no + '为非收货确认状态的订单，无法进行收货确认，请确认订单状态！', 2500);
					return;
				}
				ids += rows[i].id + ",";
			}
			$.post('order.do?checkOrderItems', {
				ids : ids,
				actionStatus:actionStatus
			}, function(result) {
				if (result.success) {
					reloadOrder();
				}
				showMsg(result.msg);
			}, 'json');
		} else {
			showMsg("请选择操作记录.");
		}
	}
	
	
	function outboundOrder(actionStatus) {
// 		var rows = order_list.datagrid('getChecked');
// 		if (rows && rows.length > 0) {
// 			var ids = "";
// 			for ( var i = 0; i < rows.length; i++) {
// 				if (rows[i].status != '3'&&actionStatus==4) {
// 					showMsg(rows[i].no + '为非待出库审核状态的订单，无法进行出库审核，请确认订单状态！', 2500);
// 					return;
// 				}
// 				ids += rows[i].id + ",";
// 			}
			
// 			$.post('order.do?checkOrderItems', {
// 				ids : ids,
// 				actionStatus:actionStatus
// 			}, function(result) {
// 				if (result.success) {
// 					reloadOrder();
// 				}
// 				showMsg(result.msg);
// 			}, 'json');
// 		} else {
// 			showMsg("请选择操作记录.");
// 		}
	}
	
	//退单  
	function chargebackOrder() {
		var rows = order_list.datagrid('getChecked');
		if (rows && rows.length > 0) {
			var ids = "";
			for ( var i = 0; i < rows.length; i++) {
				ids += rows[i].id + ",";
				if (rows[i].status != '5') {
					//非待收货确认无法退单
					showMsg(rows[i].no + '为非待收货状态的订单，无法进行退单，请确认订单状态！', 2500);
					return;
				}
			}
			$.post('order.do?chargebackOrder', {
				ids : ids
			}, function(result) {
				if (result.success) {
					reloadOrder();
				}
				showMsg(result.msg);
			}, 'json');
		} else {
			showMsg("请选择操作记录.");
		}
	}
	
	function cancleCheckPassOrder() {
		var rows = order_list.datagrid('getChecked');
		if (rows && rows.length > 0) {
			var ids = "";
			for ( var i = 0; i < rows.length; i++) {
				if (rows[i].status == '6') {
					showMsg(rows[i].no + '已完成，无法作废，请确认订单状态！', 2500);
					return;
				}
				ids += rows[i].id + ",";
			}
			$.post('order.do?cancleCheckOrderItems', {
				ids : ids
			}, function(result) {
				if (result.success) {
					reloadOrder();
				}
				showMsg(result.msg);
			}, 'json');
		} else {
			showMsg("请选择操作记录.");
		}
	}
	
	function confirmReceipt() {
		var rows = order_list.datagrid('getChecked');
		if (rows && rows.length > 0) {
			if(rows.length>1) {
				showMsg('请选择一条操作记录.');
				return;
			}
			
// 			var ids = "";
// 			for ( var i = 0; i < rows.length; i++) {
				if (rows[0].status != '6') {
					showMsg(rows[0].no + '未完成，无法确认付款，请确认订单状态！', 2500);
					return;
				}
				if (rows[0].receivables != '1') {
					showMsg(rows[0].no + '已确认付款，等待收款确认！', 2500);
					return;
				}
// 				ids += rows[i].id + ",";
// 			}
			
			createFromWindow("付款信息", "order.do?confirmReceiptPage&orderId=" + rows[0].id,
					"order.do?confirmReceipt", function(data) {
						reloadOrder();
					},true,400,200);
			
// 			$.post('order.do?confirmReceipt', {
// 				ids : ids
// 			}, function(result) {
// 				if (result.success) {
// 					reloadOrder();
// 				}
// 				showMsg(result.msg);
// 			}, 'json');
		} else {
			showMsg("请选择操作记录.");
		}
	}
	
	
	function onDblClickReturnOrderRow(rowIndex, rowData) {
		createFromWindowOptions({
			title:"编辑信息",
			maximizable:true,
			width:1300,
			height:600,
			href:"order.do?editOrder&orderId=" + rowData.id,
			callback:function(d) {
				var data = d.form("getData");
				var itemDatas = $('#orderItem_list').datagrid('getRows');
				if(itemDatas.length==0) {
					showMsg('请添加单据内容');
				}else {
					data.items = itemDatas;
					 $.post('order.do?createOrder',{data:JSON.stringify(data)},function(result){
		                    if (result.success){
		                    	reloadOrder();    
		                    }
		                    showMsg(result.msg);
		                },'json');
				}
			},
			enableSaveButton:rowData.status=='1'
		});
	}
	
	function exportOrder() {
		var rows = order_list.datagrid('getChecked');
        downLoadFile("order.do?export&orderType=normal");
	}
	
	function setTranInfo() {
		var rows = order_list.datagrid('getChecked');
		printLog(rows);
		if (rows&&rows.length>0) {
			if(rows.length>1) {
				showMsg('请选择一条操作记录.');
				return;
			}
// 			createFromWindowOptions({
// 				title:"物流信息",
// 				maximizable:true,
// 				width:1050,
// 				height:600,
// 				href:"order.do?setTransInfoPage&orderId=" + rowData.id,
// 				callback:function(d) {
// 					var data = d.form("getData");
// 					var itemDatas = $('#orderItem_list').datagrid('getRows');
// 					if(itemDatas.length==0) {
// 						showMsg('请添加单据内容');
// 					}else {
// 						data.items = itemDatas;
// 						 $.post('order.do?createOrder',{data:JSON.stringify(data)},function(result){
// 			                    if (result.success){
// 			                    	reloadOrder();    
// 			                    }
// 			                    showMsg(result.msg);
// 			                },'json');
// 					}
// 				},
// 				enableSaveButton:rowData.status=='1'
// 			});
			
			createFromWindow("物流信息", "order.do?setTransInfoPage&orderId=" + rows[0].id,
					"order.do?setTransInfo", function(data) {
						reloadOrder();
					},true,400,200);
// 			if(rows[0].status!='1') {
// 				showMsg('非创建状态订单无法编辑，请确认订单状态!',2500);
// 				return;
// 			}
			
		} else {
			showMsg('请选择操作记录.');
		}
	}
</script>

<div class="easyui-layout" data-options="fit:true">
	<div data-options="region:'center',border:false">
		
		<@eu.datagrid id="order_list" toolbar="#orderlist_search" idField="id" 
			 url="order.do?loadList&orderType=normal" rownumbers="true" onDblClickRow="onDblClickReturnOrderRow(rowIndex, rowData)"
			 item="com.xdtech.ad.vo.OrderItem"/>
		
		<div id="orderlist_search">
			<div style="margin-bottom:5px;padding-left:5px;margin-top: 5px;">
				<form id="orderSearchForm">
					<!-- 防止form表单里只有一个text元素时候，按enter键自动提交问题 -->
					<input style="display:none" />
					<input id="filterComplete" name="filter" type="hidden" />
					<table>
						<tr>
							<td>名称：</td>
							<td><input class="easyui-validatebox textbox" name="name" /></td>
							<td>
								<@eu.linkbutton showName="查询" iconCls="icon-search" onclick="orderSearch()"/>
	 					 		<@eu.linkbutton showName="清空" iconCls="icon-clear" onclick="clearOrderSearch()"/>
 					 		</td>
						</tr>
					</table>
				</form>
				
			</div>
			<div id="order_toolbar" style="height: auto">
				<@eu.linkbutton showName="新增" shiro="ad:order:add" iconCls="icon-add" onclick="addOrder()"/>
				<@eu.linkbutton showName="删除" shiro="ad:order:delete" iconCls="icon-remove" onclick="deleteOrder()"/>
				<@eu.linkbutton showName="编辑" shiro="ad:order:edit" iconCls="icon-edit" onclick="editOrder()"/>
				<@eu.linkbutton showName="订单审核" shiro="ad:order:orderCheckPass" iconCls="icon-edit" onclick="checkOrder(2)"/>
				<@eu.linkbutton showName="财务审核" shiro="ad:order:financeCheckPass" iconCls="icon-edit" onclick="checkOrder(3)"/>
				<@eu.linkbutton showName="出库审核" shiro="ad:order:outCheckPass" iconCls="icon-edit" onclick="checkOrder(4)"/>
				<@eu.linkbutton showName="发货确认" shiro="ad:order:sendCheckPass" iconCls="icon-edit" onclick="checkOrder(5)"/>
				<@eu.linkbutton showName="物流信息" shiro="ad:order:setTranInfo" iconCls="icon-edit" onclick="setTranInfo()"/>
				<@eu.linkbutton showName="收货确认" shiro="ad:order:receiveCheckPass" iconCls="icon-edit" onclick="checkOrder(6)"/>
				<@eu.linkbutton showName="退单" shiro="ad:order:chargebackOrder" iconCls="icon-edit" onclick="chargebackOrder()"/>
				<@eu.linkbutton showName="确认付款" shiro="ad:order:confirmReceipt" iconCls="icon-edit" onclick="confirmReceipt()"/>
				<@eu.linkbutton showName="作废" shiro="ad:order:cancleOrder" iconCls="icon-edit" onclick="cancleCheckPassOrder()"/>
				<@eu.linkbutton showName="打印" shiro="ad:order:printOrder" iconCls="icon-edit" onclick="printOrder()"/>
				<@eu.linkbutton showName="查看跟踪" shiro="ad:order:tracingOrder" iconCls="icon-edit" onclick="tracingOrder()"/>				
				<@eu.linkbutton showName="订单导出" shiro="ad:order:export" iconCls="icon-export" onclick="exportOrder()"/>
				<@eu.linkbutton showName="刷新" shiro="ad:order:refresh" iconCls="icon-reload" onclick="reloadOrder()"/>
			</div>
		</div>
	</div>
</div>
<script>
	if(window.tabsFilter) {
		$('#filterComplete').val(window.tabsFilter);
		setTimeout(function() {orderSearch();$('#filterComplete').val('');},200);
	}
	window.tabsFilter = undefined;
	
	if(window.showAddOrderWindow) {
		addOrder();
		window.showAddOrderWindow=undefined;
	}
</script>